package com.bdmc.smokesensor.controller;

import java.util.ArrayList;
import java.util.Map;
import com.bdmc.smokesensor.Utils.Msg;
import com.bdmc.smokesensor.Utils.ResultUtil;
import com.bdmc.smokesensor.entity.Rbac_Action;
import com.bdmc.smokesensor.entity.Sys_Log;
import com.bdmc.smokesensor.services.Rbac_ActionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping(value = "/action",method = RequestMethod.POST)
public class Rbac_ActionController extends BaseController{

    @Autowired
    private Rbac_ActionService _actionSvs;


    @RequestMapping(value = "/list")
    public Msg list(@RequestBody Map<String,Object> reqMap) {
        //int page = Integer.valueOf(reqMap.get("page").toString());
        return ResultUtil.success( _actionSvs.findAll());
    }

    /**
     * 数据新增或更新，save方法可以执行添加也可以执行更新，如果需要执行持久化的实体存在主键值则更新数据，如果不存在则添加数据。
     */
    @RequestMapping(value = "/save")
    public Msg save(@RequestBody Rbac_Action fun) {
        String action = fun.getId()>0?"修改:":"新增:";
        Rbac_Action f = _actionSvs.save(fun);
        if(f!=null&&f.getId()>0){
            Sys_Log log = new Sys_Log(getLoginName(), getLocalIp() , "按钮", action+fun.getActionName());
            _logSvs.save(log);
            return ResultUtil.success();
        }else
        {
            return ResultUtil.error(-1,"操作失败，请联系管理员。");
        }
        
    }

     /**
     * 删除方法
     */
    @RequestMapping(value = "/delete")
    public Msg delete(@RequestBody Map<String, String> map) {
        int id= Integer.parseInt(map.get("id"));
        _actionSvs.deleteById(id);
        Sys_Log log = new Sys_Log(getLoginName(), getLocalIp() , "按钮", "删除："+id);
            _logSvs.save(log);
        return ResultUtil.success();
    }

     /**
     * 根据function获取action
     */
    @RequestMapping(value = "/getbyfunid")
    public Msg getParentFunction(@RequestBody Map<String, String> map) {
        int id= Integer.parseInt(map.get("id"));
        return ResultUtil.success(_actionSvs.GetByfunctionId(id));
    }

   




}
